<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  
<%@ page import="org.apache.commons.fileupload.disk.*"%>  
<%@ page import="org.apache.commons.fileupload.servlet.*"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Register</title>
        <link rel="stylesheet" type="text/css" title="T1" href="stylesheet/progin.css" id="link_theme" />
	<link rel="stylesheet" type="text/css" title="T1" href="stylesheet/posting.css" />
		<meta name="description" content="RERGag, Make You Laughing" />
		<meta name="keywords" content="laugh" />
		<meta name="author" content="Robertus Theodore" />
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
        <h1>Hello World!</h1>
        <div class="main_container">
            <div id="preview"></div>
	
            <div align="left" style="width: 500px;" >
                <p>Please Upload your photo First!</p>
                <form id="posting_form" name="posting_form" method="post" action="registerHandler.jsp" >
                    <fieldset><legend>Register Baru</legend>

                            <label class="label_posting">username </label><input id="form_nama" class="input_posting" name="username" type="text"/><br/>

                            <label class="label_posting">password</label><input class="input_posting" id="password" type="password" name="password"/><br />
                            <label class="label_posting">confirm password</label><input class="input_posting" id="cpassword" type="password" name="cpassword"/><br />

                            <label class="label_posting">e-mail</label><input class="input_posting" id="email" type="text" name="email"/><br />
                            <label class="label_posting">confirm e-mail</label><input class="input_posting" id="cemail" type="text" name="cemail"/><br />

                            <label class="label_posting">Tanggal Lahir</label><input class="input_posting" id="birthdate" type="text" name="birthdate"/><br />

                            <label class="label_posting">About Me</label><textarea class="textarea_posting" id="about_me" name="about_me" rows="5" cols="25"></textarea><br />

                            <label class="label_posting">Gender </label><input id="option_link" class="input_posting" type="radio" name="gender" value="male"/>Male<br />
                            <label class="label_posting"></label><input id="option_image" class="input_posting" type="radio" name="gender" value="female"/>Female<br />
                            <label class="label_posting">&nbsp;</label><input type="submit" class="input_posting" value="Register"/>

                </form>
                        
                
                <form id="posting_form2" name="posting_form2" method="post" action="register.jsp" enctype="multipart/form-data">
                    <label class="label_posting">Upload Your Avatar </label><input class="input_posting" name="avatar" type="file" /><br />
                    <label class="label_posting">&nbsp;</label><input type="Submit" class="input_posting" value="Upload File"/>
                </form>
                </fieldset>            

        <%!	  
	//Uploading File -- method to return file extension  
	String getFileExt(String xPath){  
  
            //Find extension  

            int dotindex = 0;	//extension character position  
            dotindex = xPath.lastIndexOf('.');  

            if (dotindex == -1){	// no extension	  
                return "";  
            }  

            int slashindex = 0;	//seperator character position  
            slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));  

            if (slashindex == -1){	// no seperator characters in string  
                return xPath.substring(dotindex);  
            }  

            if (dotindex < slashindex){	//check last "." character is not before last seperator  
                return "";  
            }  
            return xPath.substring(dotindex);  
	}
        %>

        <%  // Form Processing
            //to get the content type information from JSP Request Header
            String contentTypeFile = request.getContentType();
            // Check that we have a file upload request
                    if((contentTypeFile!= null) && (contentTypeFile.indexOf("multipart/form-data") >= 0)){
                        boolean isMultipart = FileUpload.isMultipartContent(request);  
                        //out.println("isMultipart="+isMultipart+"<br>");  

                        //Create variables for path, filename and extension  
                        //String newFilePath = application.getRealPath("/") + "images";  
                        //String newFilePath = "images/posting";  
                        String newFilePath = "C:/Users/Robert/Documents/NetBeansProjects/reryes/web/images/avatar";
                        String pathToDatabase = "images/avatar";
                        String newFileName ="";     // file named with id content
                        String FileExt = "";  

                        // Create a factory for disk-based file items  
                        FileItemFactory factory = new DiskFileItemFactory();  

                        // Create a new file upload handler  
                        ServletFileUpload upload = new ServletFileUpload(factory);  

                        // Parse the request  
                        List /* FileItem */ items = upload.parseRequest(request);  

                        // Process the uploaded items  
                        Iterator iter = items.iterator();  
                        //Form fields   
                        while (iter.hasNext()) {
                            FileItem item = (FileItem) iter.next();  
                            newFileName = item.getName();
                            /* item already exist in directory
                            if (item.isFormField()) {  
                                String name = item.getFieldName();  
                                String value = item.getString();  
                                if (name.equals("newFileName")) {  
                                        newFileName = value;  
                                }  
                                out.println("Form Field Name= " + name + "<br>");  
                                out.println("Form Field Value= " + value + "<br><br>");  
                            } 
                            */  
                             
                            String fieldName = item.getFieldName();  
                            String fileName = item.getName();  
                            FileExt = getFileExt(fileName);  
                            String contentType = item.getContentType(); 
                            // check file extension 
                            if (!contentType.equals("image/jpeg")){
                                out.println("Ekstensi File bukan Bertipe .jpeg");
                                //response.sendRedirect("testUpload.jsp");
                            }
                            
                            // extension valid
                            else {
                                boolean isInMemory = item.isInMemory();  
                                long sizeInBytes = item.getSize();  
                                //out.println("fieldName= " + fieldName + "<br>");  
                                //out.println("fileName= " + fileName + "<br>");  
                                //out.println("FileExt= " + FileExt + "<br>");  
                                //out.println("contentType= " + contentType + "<br>");  
                                //out.println("isInMemory= " + isInMemory + "<br>");  
                                //out.println("sizeInBytes= " + sizeInBytes + "<br>");
                                //out.println("path  = "+ newFilePath + "<br>");
                                if (fileName.equals("") || sizeInBytes==0){  
                                        out.println("Not a valid file.<br>No upload attempted.<br><br>");  
                                } else {  
                                    File uploadedFile = new File(newFilePath+"/", newFileName+FileExt);  
                                    try{  
                                            item.write(uploadedFile);  
                                            //out.println( newFilePath +"\\"+newFileName+FileExt+ "\"");
                                            //out.println(fileName+" was successfully uploaded <br><br>");  
                                            //out.println("<input type=\"button\" name=\"path\" value=\""+ newFilePath +"/"+newFileName+"\"></input>");
                                            String pathFile = pathToDatabase+"/"+newFileName;
                                            out.println(pathFile);
                                            session.setAttribute("pathImage", pathFile);
                                    }  
                                    catch (java.lang.Exception e) {  
                                            out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br><br>");  
                                    }  
                                }  
                            } 
                        }
                    }
            %>
 
            </div>
        <script type="text/javascript" src="script/progin.js"></script>
        </div>
    </body>
</html>
